﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.BLL.GlobalCommands;
using office = Microsoft.Office.DocumentManagement.DocumentSets;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.BLL.CaptureOutcomeRadicado
{
    public class CaptureOutcomeProcess : IBaseCommand
    {

        #region fields

        Stack<IBaseCommand> _commands;
        int _radicadoItemId;
        FileEntity _fileContent;
        string _radicadoLibraryName;

        #endregion

        #region Constructor

        public CaptureOutcomeProcess(string documentLibrary, int itemId, FileEntity fileContent)
        {
            _radicadoItemId = itemId;
            _fileContent = fileContent;
        }

        #endregion

        #region Implemented Methods

        public void Execute()
        {
            try
            {
                //Configuring transaction execution
                office.DocumentSet selectedDocument = RadicadosHelper.GetDocumentSetById(_radicadoItemId, _radicadoLibraryName);
                _commands = new Stack<IBaseCommand>();

                //Load Captured Document
                LoadCapturedImageRadicadoCommand loadCommand = new LoadCapturedImageRadicadoCommand(_fileContent, selectedDocument);
                loadCommand.Execute();
                _commands.Push(loadCommand);

                //Change state command
                ChangeStateCommand changeStateCommand = new ChangeStateCommand(selectedDocument, GlobalConstants.METADATA_TERM_STATUS_CAPTURED);
                changeStateCommand.Execute();
                _commands.Push(changeStateCommand);
            }
            catch (Exception ex)
            {
                Undo();
                throw ex;
            }

        }

        public void Undo()
        {
            foreach (IBaseCommand command in _commands)
                command.Undo();
        }

        #endregion


    }
}
